package cn.mldn.microboot.controller;

import cn.mldn.microboot.util.controller.AbstractBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

/**
 * description:
 * Created by gy on 2017/8/13.
 */
@Controller
public class UploadContorller extends AbstractBaseController {

    @RequestMapping(value = "/uploadPre", method = RequestMethod.GET)
    public String uploadPre() {
        return "upload_page";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(String name,/* MultipartFile file,*/ HttpServletRequest request) {
        //多文件上传
        if(request instanceof MultipartHttpServletRequest){

            MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = mrequest.getFiles("file");
            Iterator<MultipartFile> iterator = files.iterator();
            while(iterator.hasNext()){
                MultipartFile file = iterator.next();
                if (!file.isEmpty()) {
                    System.out.println("file is uploading " + name);
                    System.out.println("file name " + file.getOriginalFilename());
                    System.out.println("file content type " + file.getContentType());
                }
            }


        }
        //单个文件上传
//            if (!file.isEmpty()) {
//                System.out.println("file is uploading " + name);
//                System.out.println("file name " + file.getName());
//                System.out.println("file content type " + file.getContentType());
//            }
        return "upload_file";
    }
}
